/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/util/enums/Entities.java.hbs
 */
package cn.ibizlab.util.enums;

import cn.ibizlab.util.domain.IEntity;
import cn.ibizlab.util.filter.ISearchContext;
import cn.ibizlab.util.helper.BeanCache;
import cn.ibizlab.util.security.SpringContextHolder;
import lombok.SneakyThrows;
import java.util.Arrays;
import java.util.Optional;

/**
 * 注册实体枚举
 *
 * @author generator
 */
public enum Entities {

    AUTH_GRANTED ("AuthGranted","授权对象",cn.ibizlab.core.authentication.domain.AuthGranted.class,null,null),
    AUTH_GRANTED_ROLE ("AuthGrantedRole","授权角色",cn.ibizlab.core.authentication.domain.AuthGrantedRole.class,null,null),
    AUTH_GRANTED_SYSTEM ("AuthGrantedSystem","授权系统",cn.ibizlab.core.authentication.domain.AuthGrantedSystem.class,null,null),
    AUTH_INFO ("AuthInfo","认证信息",cn.ibizlab.core.authentication.domain.AuthInfo.class,null,null),
    AUTH_LOG ("AuthLog","认证日志",cn.ibizlab.core.authentication.domain.AuthLog.class,cn.ibizlab.core.authentication.filter.AuthLogSearchContext.class,cn.ibizlab.core.authentication.service.AuthLogService.class),
    AUTH_LOGIN ("AuthLogin","登录信息",cn.ibizlab.core.authentication.domain.AuthLogin.class,cn.ibizlab.core.authentication.filter.AuthLoginSearchContext.class,cn.ibizlab.core.authentication.service.AuthLoginService.class),
    AUTH_MEMBER ("AuthMember","隶属信息",cn.ibizlab.core.authentication.domain.AuthMember.class,null,null),
    AUTH_PROVIDER ("AuthProvider","统一认证/外部认证",cn.ibizlab.core.authentication.domain.AuthProvider.class,cn.ibizlab.core.authentication.filter.AuthProviderSearchContext.class,cn.ibizlab.core.authentication.service.AuthProviderService.class),
    AUTH_USER ("AuthUser","认证用户",cn.ibizlab.core.authentication.domain.AuthUser.class,cn.ibizlab.core.authentication.filter.AuthUserSearchContext.class,cn.ibizlab.core.authentication.service.AuthUserService.class),
    OAUTH_ACCESS_TOKEN ("OauthAccessToken","认证授权Token记录",cn.ibizlab.core.oauth2.domain.OauthAccessToken.class,cn.ibizlab.core.oauth2.filter.OauthAccessTokenSearchContext.class,cn.ibizlab.core.oauth2.service.OauthAccessTokenService.class),
    OAUTH_CLIENT_DETAILS ("OauthClientDetails","oauth客户端信息",cn.ibizlab.core.oauth2.domain.OauthClientDetails.class,cn.ibizlab.core.oauth2.filter.OauthClientDetailsSearchContext.class,cn.ibizlab.core.oauth2.service.OauthClientDetailsService.class),
    OAUTH_REFRESH_TOKEN ("OauthRefreshToken","RefreshToken",cn.ibizlab.core.oauth2.domain.OauthRefreshToken.class,cn.ibizlab.core.oauth2.filter.OauthRefreshTokenSearchContext.class,cn.ibizlab.core.oauth2.service.OauthRefreshTokenService.class),
    OPEN_ACCESS ("OpenAccess","开放接入",cn.ibizlab.core.open.domain.OpenAccess.class,cn.ibizlab.core.open.filter.OpenAccessSearchContext.class,cn.ibizlab.core.open.service.OpenAccessService.class),
    OPEN_ACCESS_AUTH ("OpenAccessAuth","接入认证绑定",cn.ibizlab.core.open.domain.OpenAccessAuth.class,cn.ibizlab.core.open.filter.OpenAccessAuthSearchContext.class,cn.ibizlab.core.open.service.OpenAccessAuthService.class),
    SYS_ACTIVE_DIRECTORY ("SysActiveDirectory","AD",cn.ibizlab.core.ad.domain.SysActiveDirectory.class,cn.ibizlab.core.ad.filter.SysActiveDirectorySearchContext.class,cn.ibizlab.core.ad.service.SysActiveDirectoryService.class),
    SYS_API ("SysApi","接口",cn.ibizlab.core.system.domain.SysApi.class,null,null),
    SYS_API_OPERATION ("SysApiOperation","接口方法",cn.ibizlab.core.system.domain.SysApiOperation.class,null,null),
    SYS_COMMON_NAME ("SysCommonName","CN",cn.ibizlab.core.ad.domain.SysCommonName.class,cn.ibizlab.core.ad.filter.SysCommonNameSearchContext.class,cn.ibizlab.core.ad.service.SysCommonNameService.class),
    SYS_COMPONENT ("SysComponent","组件",cn.ibizlab.core.system.domain.SysComponent.class,null,null),
    SYS_COMPONENT_API ("SysComponentApi","组件使用接口",cn.ibizlab.core.system.domain.SysComponentApi.class,null,null),
    SYS_CUSTOM_RESOURCE ("SysCustomResource","自定义资源",cn.ibizlab.core.system.domain.SysCustomResource.class,null,null),
    SYS_DATA_SCOPE ("SysDataScope","数据范围",cn.ibizlab.core.system.domain.SysDataScope.class,null,null),
    SYS_DEPARTMENT ("SysDepartment","部门",cn.ibizlab.core.ad.domain.SysDepartment.class,cn.ibizlab.core.ad.filter.SysDepartmentSearchContext.class,cn.ibizlab.core.ad.service.SysDepartmentService.class),
    SYS_DEPLOY_SYSTEM ("SysDeploySystem","系统",cn.ibizlab.core.system.domain.SysDeploySystem.class,cn.ibizlab.core.system.filter.SysDeploySystemSearchContext.class,cn.ibizlab.core.system.service.SysDeploySystemService.class),
    SYS_DOMAIN ("SysDomain","域",cn.ibizlab.core.ad.domain.SysDomain.class,cn.ibizlab.core.ad.filter.SysDomainSearchContext.class,cn.ibizlab.core.ad.service.SysDomainService.class),
    SYS_GRANTED_AUTHORITY ("SysGrantedAuthority","角色授权资源",cn.ibizlab.core.authorization.domain.SysGrantedAuthority.class,cn.ibizlab.core.authorization.filter.SysGrantedAuthoritySearchContext.class,cn.ibizlab.core.authorization.service.SysGrantedAuthorityService.class),
    SYS_GROUP_OF_NAMES ("SysGroupOfNames","GroupOfNames",cn.ibizlab.core.ad.domain.SysGroupOfNames.class,cn.ibizlab.core.ad.filter.SysGroupOfNamesSearchContext.class,cn.ibizlab.core.ad.service.SysGroupOfNamesService.class),
    SYS_MEMBER ("SysMember","成员",cn.ibizlab.core.ad.domain.SysMember.class,cn.ibizlab.core.ad.filter.SysMemberSearchContext.class,cn.ibizlab.core.ad.service.SysMemberService.class),
    SYS_MENU ("SysMenu","系统菜单",cn.ibizlab.core.system.domain.SysMenu.class,null,null),
    SYS_MENU_FUNC ("SysMenuFunc","菜单功能",cn.ibizlab.core.system.domain.SysMenuFunc.class,null,null),
    SYS_ORGANIZATION ("SysOrganization","组织机构",cn.ibizlab.core.ad.domain.SysOrganization.class,cn.ibizlab.core.ad.filter.SysOrganizationSearchContext.class,cn.ibizlab.core.ad.service.SysOrganizationService.class),
    SYS_ORGANIZATIONAL_ROLE ("SysOrganizationalRole","用户组",cn.ibizlab.core.ad.domain.SysOrganizationalRole.class,cn.ibizlab.core.ad.filter.SysOrganizationalRoleSearchContext.class,cn.ibizlab.core.ad.service.SysOrganizationalRoleService.class),
    SYS_ORGANIZATIONAL_UNIT ("SysOrganizationalUnit","组织单元",cn.ibizlab.core.ad.domain.SysOrganizationalUnit.class,cn.ibizlab.core.ad.filter.SysOrganizationalUnitSearchContext.class,cn.ibizlab.core.ad.service.SysOrganizationalUnitService.class),
    SYS_PERMISSION ("SysPermission","权限资源",cn.ibizlab.core.system.domain.SysPermission.class,cn.ibizlab.core.system.filter.SysPermissionSearchContext.class,cn.ibizlab.core.system.service.SysPermissionService.class),
    SYS_PERSON ("SysPerson","人员",cn.ibizlab.core.ad.domain.SysPerson.class,cn.ibizlab.core.ad.filter.SysPersonSearchContext.class,cn.ibizlab.core.ad.service.SysPersonService.class),
    SYS_ROLE ("SysRole","系统角色",cn.ibizlab.core.authorization.domain.SysRole.class,cn.ibizlab.core.authorization.filter.SysRoleSearchContext.class,cn.ibizlab.core.authorization.service.SysRoleService.class),
    SYS_ROLE_MEMBER ("SysRoleMember","系统角色成员",cn.ibizlab.core.authorization.domain.SysRoleMember.class,cn.ibizlab.core.authorization.filter.SysRoleMemberSearchContext.class,cn.ibizlab.core.authorization.service.SysRoleMemberService.class),
    TEST ("Test","测试实体",cn.ibizlab.core.ad.domain.Test.class,cn.ibizlab.core.ad.filter.TestSearchContext.class,cn.ibizlab.core.ad.service.TestService.class)
    ;

    public final String codeName;
    public final String displayName;
    public final Class<? extends IEntity> entityClass;
    public final Class<? extends ISearchContext> filterClass;
    public final Class<?> serviceClass;

    public BeanCache.BeanSchema getSchema() {
        return BeanCache.get(entityClass);
    }

    @SneakyThrows
    public IEntity newEntityInstance() {
        return entityClass.newInstance();
    }

    @SneakyThrows
    public ISearchContext newFilterInstance() {
        return filterClass.newInstance();
    }

    @SneakyThrows
    public <T> T getService() {
        return (T) SpringContextHolder.getBean(serviceClass);
    }

    private Entities(String codeName, String displayName, Class<? extends IEntity> entityClass, Class<? extends ISearchContext> filterClass, Class serviceClass){
        this.codeName = codeName;
        this.displayName = displayName;
        this.entityClass = entityClass;
        this.filterClass = filterClass;
        this.serviceClass = serviceClass;
    }

    public static Optional<Entities> from(String codeName) {
        return Arrays.stream(Entities.values()).filter(item -> item.codeName.equalsIgnoreCase(codeName)||item.name().equalsIgnoreCase(codeName)||item.codeName.equalsIgnoreCase(codeName.replace("_",""))||item.name().equalsIgnoreCase(codeName+"DTO")||item.name().equalsIgnoreCase(codeName+"FilterDTO")).findFirst();
    }

    @Override
    public String toString() {
        return codeName;
    }

    public boolean equals(String codeName) {
        return this.codeName.equalsIgnoreCase(codeName)||this.name().equalsIgnoreCase(codeName)||this.codeName.equalsIgnoreCase(codeName.replace("_",""));
    }
}